package com.marketingexpert.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.marketingexpert.domain.User;
import com.marketingexpert.domain.UserType;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;

public class WebUtils {
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static Calendar c  = Calendar.getInstance();
	
	private static Random random = new Random();
	
	public static boolean compareDate(Date start, Date end) {
		int result = start.compareTo(end);		
		if(result < 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean compareDate2(Date start, Date end) {
		int result = end.compareTo(start);		
		if(result <= 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public static String getBasepath(HttpServletRequest request) {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path;
		return basePath;
	}
	
	public static String getStartFormatDate(){
		Date d = new Date();
		return sdf.format(d);
	}
	
	public static String getEndFormatDate(int year, int month, int date) {
		c.set(year, month, date);
		Date d = c.getTime();
		return sdf.format(d);
	}
	
	public static int getCurrentMonth(){
		return c.get(c.MONTH)+1;
	}
	
	public  static int getCurrentDay() {
		return c.get(c.DAY_OF_MONTH);
	}
	
	public static int getCurrentYear(){
		return c.get(c.YEAR);
	}
	
	/**
	 * ����û�����ȷ��snsService����
	 * @param user
	 * @param snsServiceFactory
	 * @return
	 */
	public static SnsService getSnsService(User user, SnsServiceFactory snsServiceFactory) {
		if(user.getUserType().equals(UserType.SINAWEIBO)) {
			return snsServiceFactory.getService(SnsServiceFactory.SINA);
		} else if(user.getUserType().equals(UserType.TENCENTWEIBO)) {
			return snsServiceFactory.getService(SnsServiceFactory.TECENT);
		} else {
			return null;
		}
	}
	
	/**
	 * 判断日期是不是当天
	 * @return
	 */
	public static boolean isToday(Date time) {
		try {
			StringBuffer todayStartTimeStr = new StringBuffer();
			todayStartTimeStr.append(c.get(Calendar.YEAR)).append("-")
							  .append(c.get(Calendar.MONTH) + 1).append("-")
							  .append(c.get(Calendar.DATE)).append(" 00:00:00");
			Date todayStartTime = sdf2.parse(todayStartTimeStr.toString());	
			
			StringBuffer todayEndStr = new StringBuffer();
			todayEndStr.append(c.get(Calendar.YEAR)).append("-")
			  				 .append(c.get(Calendar.MONTH) + 1).append("-")
			  				 .append(c.get(Calendar.DATE)).append(" 23:59:59");
			Date todayEndTime = sdf2.parse(todayEndStr.toString());
			
			boolean isToday = time.after(todayStartTime) && time.before(todayEndTime); 
			
			return isToday;			
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static int getRondom() {
		return random.nextInt(9876599);
	}
}
